package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


import java.time.LocalDateTime;

//自定义元数据对象处理器
@Component
public class MyMateObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Long id = UserHolder.getUserId();

        if (metaObject.hasSetter("createTime")) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("createUser")) {
            metaObject.setValue("createUser", id);
        }

        if (metaObject.hasSetter("updateUser")) {
            metaObject.setValue("updateUser", id);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = UserHolder.getUserId();

        if (metaObject.hasSetter("updateTime")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }

        if (metaObject.hasSetter("updateUser")) {
            metaObject.setValue("updateUser", id);
        }

    }
}
